package de.openkanban.client.ui.adminpanel;

import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.layout.VLayout;

import de.openkanban.client.ui.board.BoardHeaderButton;

/**
 * GUI Element for the UserAccountWindow
 */
public class UserAccountWindow extends Window {

	private static final int FORM_WIDTH = 450;

	public UserAccountWindow() {
		VLayout vlMain = new VLayout();

		UserAccountForm userAccountForm = new UserAccountForm(FORM_WIDTH);
		UserAccountPasswordForm userAccountPasswordForm = new UserAccountPasswordForm(FORM_WIDTH);
		UserAccountAvatarForm userAccountAvatarForm = new UserAccountAvatarForm(FORM_WIDTH);

		IButton submitAccountButton = new BoardHeaderButton("update account");
		submitAccountButton.addClickHandler(new SubmitFormClickHandler(userAccountForm));
		submitAccountButton.setLeft(40);

		IButton submitPasswordButton = new BoardHeaderButton("update password");
		submitPasswordButton.addClickHandler(new SubmitFormClickHandler(userAccountPasswordForm));
		submitPasswordButton.setLeft(40);

		IButton submitAvatarButton = new BoardHeaderButton("update avatar");
		submitAvatarButton.addClickHandler(new SubmitFormClickHandler(userAccountAvatarForm));
		submitPasswordButton.setLeft(40);

		setTitle("User Account");
		setAutoSize(true);
		setAutoCenter(true);
		setShowMinimizeButton(false);
		setShowMaximizeButton(false);
		setIsModal(true);
		setShowModalMask(true);

		vlMain.setPadding(5);
		vlMain.addMember(userAccountForm);
		vlMain.addMember(submitAccountButton);

		vlMain.addMember(userAccountPasswordForm);
		vlMain.addMember(submitPasswordButton);

		vlMain.addMember(userAccountAvatarForm);
		vlMain.addMember(submitAvatarButton);

		addItem(vlMain);
	}

	class SubmitFormClickHandler implements ClickHandler {

		private DynamicForm form;

		public SubmitFormClickHandler(DynamicForm form) {
			super();
			this.form = form;
		}

		@Override
		public void onClick(ClickEvent event) {
			form.submit();
		}

	}

}
